/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller.c;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.UserTypeEnum;
import com.ykht.proj.coupon.entity.CnBrand;
import com.ykht.proj.coupon.entity.CnCompany;
import com.ykht.proj.coupon.service.CnBrandService;
import com.ykht.proj.coupon.service.CnCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 公司表
 *
 * @author proj code generator
 * @date 2020-11-17 14:05:06
 */
@RestController
@AllArgsConstructor
@RequestMapping("c/cncompany")
@Api(value = "cncompany", tags = "c端公司相关")
public class CompanyController {

	private final CnCompanyService cnCompanyService;

	private final CnBrandService cnBrandService;

	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param cnCompany 公司表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R getCnCompanyPage(Page page, CnCompany cnCompany) {
		return R.ok(cnCompanyService.page(page, Wrappers.query(cnCompany)));
	}


	/**
	 * 通过id查询公司表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(cnCompanyService.getById(id));
	}

	/**
	 * 新增公司表
	 *
	 * @param cnCompany 公司表
	 * @return R
	 */
	@ApiOperation(value = "新增公司表", notes = "新增公司表")
	@SysLog("新增公司表")
	@PostMapping
	public R save(@RequestBody CnCompany cnCompany) {
		return R.ok(cnCompanyService.save(cnCompany));
	}

	/**
	 * 修改公司表
	 *
	 * @param cnCompany 公司表
	 * @return R
	 */
	@ApiOperation(value = "修改公司表", notes = "修改公司表")
	@SysLog("修改公司表")
	@PutMapping
	public R updateById(@RequestBody CnCompany cnCompany) {
		return R.ok(cnCompanyService.updateById(cnCompany));
	}

	/**
	 * 通过id删除公司表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除公司表", notes = "通过id删除公司表")
	@SysLog("通过id删除公司表")
	@DeleteMapping("/{id}")
	public R removeById(@PathVariable Long id) {
		return R.ok(cnCompanyService.removeById(id));
	}


	/**
	 * 通过登录人查询 公司
	 * userType  2 品牌  3门店
	 *
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/getUserType")
	public R getUserType(String userType) {
		if (userType.equals("2")) {
			List companyList = new ArrayList<>();
			if (SecurityUtils.getUser().getUserType() == UserTypeEnum.admin.getValue()) {
				companyList = cnCompanyService.list();
//	这句话看不懂什么意思，遂注释		companyList.stream().forEach(itme -> itme.setBrandName(itme.getName()));
			} else if (SecurityUtils.getUser().getUserType() == UserTypeEnum.company.getValue()) {
				List<Map> list = SecurityUtils.getUser().getSjList();
				companyList = list.stream().filter(o -> SecurityUtils.getSjId() == MapUtil.getLong(o, "id")).collect(Collectors.toList());
			}
			return R.ok(companyList);
		} else if (userType.equals("3")) {
			return R.ok(cnBrandService.list());
		}
		return R.ok();
	}

	/**
	 * 通过登录人查询 公司 品牌
	 * userType  2 品牌  3门店
	 *
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/getLoginId")
	public R getLoginId(String userType) {
		Long sjId = SecurityUtils.getSjId();
		Integer userType1 = SecurityUtils.getUser().getUserType();
		List companyList = new ArrayList<>();
		if (userType.equals("2")) {
			if (userType1 == UserTypeEnum.admin.getValue()) {
				companyList = cnCompanyService.list();
			} else if (userType1 == UserTypeEnum.company.getValue()) {
				companyList = cnCompanyService.list(Wrappers.lambdaQuery(CnCompany.class).eq(CnCompany::getId, sjId));
			}
			return R.ok(companyList);
		} else if (userType.equals("3")) {
			if (userType1 == UserTypeEnum.admin.getValue()) {
				companyList = cnBrandService.list();
			} else if (userType1 == UserTypeEnum.company.getValue()) {
				companyList = cnBrandService.list(Wrappers.lambdaQuery(CnBrand.class).eq(CnBrand::getCompanyId, sjId));
			} else if (userType1 == UserTypeEnum.brand.getValue()) {
				companyList = cnBrandService.list(Wrappers.lambdaQuery(CnBrand.class).eq(CnBrand::getId, sjId));
			}
		}
		return R.ok(companyList);
	}


	/**
	 * 根据公司ID获取品牌信息
	 *
	 * @return R
	 */
	@ApiOperation("根据公司ID获取品牌信息")
	@PostMapping("/getByCompanyId/{companyId}")
	public CnCompany getByCompanyId(@PathVariable("companyId") Long companyId) {
		return cnCompanyService.getById(companyId);
	}

}
